//
//  RRadioListController.m
//  EPAa
//
//  Created by lanou3g on 16/4/24.
//  Copyright © 2016年 李日青. All rights reserved.
//

#import "RRadioListController.h"
#import "RNetWorkRequestManager.h"
#import "RRadioList.h"
#import "RRadioListViewCell.h"
#import "CKCycleView.h"
#import "RRadioController.h"
@interface RRadioListController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic ,strong)NSMutableArray *radioListArray;
@property (nonatomic, strong)UICollectionView *collectionViews;
//轮播图属性
@property (nonatomic,strong)CKCycleView *cycleView;
@end

@implementation RRadioListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionViews.backgroundColor = [UIColor whiteColor];
    self.radioListArray = [NSMutableArray new];
    [self readFromList];
    //创建collectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionViews = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    //设置layout
    layout.itemSize = CGSizeMake(self.view.frame.size.width * 0.3, self.view.frame.size.height * 0.25);
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 200);
    self.collectionViews.delegate = self;
    self.collectionViews.dataSource = self;
    [self.view addSubview:self.collectionViews];
    //注册cell
    [self.collectionViews registerNib:[UINib nibWithNibName:@"RRadioListViewCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
    //注册头视图
    [self.collectionViews registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    self.cycleView = [[CKCycleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150) interVal:2];
    self.cycleView.imgNameArray = [@[@"h1.jpeg",@"h2.jpeg",@"h3.jpeg",@"h4.jpeg",@"h5.jpeg",@"h6.jpeg",@"h7.jpeg"] mutableCopy ];

}
- (void)readFromList {
    //解析数据
    [RNetWorkRequestManager requestURL:[NSURL URLWithString:RADIOLIST_URL] Method:CK_POST Params:    @{@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":@"",@"version":@"3.0.2"} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@",jsonDict);
            NSDictionary *dataDic = jsonDict[@"data"];
            
            for (NSDictionary *itemDict in dataDic[@"alllist"]) {
                RRadioList *model = [RRadioList new];
                [model setValuesForKeysWithDictionary:itemDict];
                [self.radioListArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionViews reloadData];
            });
        }
        
    }];
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.radioListArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RRadioListViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    RRadioList *model = self.radioListArray[indexPath.row];
    cell.model = model;
    return cell;
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        [header addSubview:self.cycleView];
        return header;
    }
    return nil;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    RRadioController *radioVC = [RRadioController new];
    RRadioList *model = self.radioListArray[indexPath.row];
    radioVC.string = model.radiood;
    [self.navigationController pushViewController:radioVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
